# -*- coding: utf-8 -*-
""" Compomer Factory class
"""

__author__ = """Alexander Artyomenko (artyomenkoav@gmail.com)"""
#   Copyright (C) 2011-2012 by
#   Alex Artyomenko <artyomenkoav@gmail.com>
#   All rights reserved.
#   BSD license.

from cfactory import CompomerFactory
from compomer import Compomer 
from IO import out
import argparse
import sys

if __name__ == '__main__':
    '''
    Program takes as input file with following format
    <cleave_nucl> // A,C,T or G
    <peak1>
    <peak2>
    ...
    <peakN> all peaks are numbers with floating point
    '''
    # setup the argument parser
    argp = argparse.ArgumentParser(description="""Populate compomers from peaks.""")
    argp.add_argument("MSFile", type=argparse.FileType("r"),\
            help="Mass spectrometry data file.")
    argp.add_argument("-o", dest="CSVFile", type=argparse.FileType("w+"),\
            help="Output data file.")

    # get the command line arguments
    args = argp.parse_args(sys.argv[1:])
    
    peaks = args.MSFile.read().split('\n')
    # get cleave nucleotide (first line)
    cleave_nucl = peaks.pop(0)
    # convert strings into numbers
    peaks = map(lambda x : float(x), peaks)
    # Generating factory contains all possible compomers less than 40 bases
    cf = CompomerFactory(40)
    # output found compomers 
    out = out(args.CSVFile)
    out.write(str(cleave_nucl + "\n"))
    # header
    out.write("Input Masses\tCompomer Mass\tA\tC\tT\tG\n")
    for l in peaks:
        comp = Compomer(l, cf.get_compomer_variants(cleave_nucl, l))
        out.write("{}\t{}".format(l,str(comp)))
    # We done here        
    print "Finished!"
    